<template>
  <div class="distribution-recruit-set gray-box top-redius border-bottom-gray">
    <div class="title">
      {{ $t("user.pointsExpirationSetting") }}
    </div>
    <el-form
      ref="dataFormRef"
      label-width="120px"
      class="set-form"
      label-position="left"
      :rules="dataRule"
      :model="dataForm"
      @submit.prevent
    >
      <el-form-item
        :label="$t('user.expirationSwitch')"
        style="width: 440px"
        prop="scoreExpireSwitch"
      >
        <el-switch v-model="dataForm.scoreExpireSwitch" />
      </el-form-item>
      <el-form-item
        v-if="dataForm.scoreExpireSwitch"
        :label="$t('user.expiration')"
        prop="expireYear"
      >
        <el-input-number
          v-model="dataForm.expireYear"
          autosize
          witdt="330"
          :min="1"
          :max="1000000"
        />
        <span>{{ $t("user.year") }}</span>
        <div>
          <span style="color: gray">{{ $t("user.expirationDes") }}</span>
          <span style="color: gray">{{ $t("user.expirationDes1") }}</span>
        </div>
      </el-form-item>
    </el-form>
    <div
      class="default-btn"
      @click="onSubmit()"
    >
      {{
        $t("sysManagement.save")
      }}
    </div>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'
const dataRule = {
  expireYear: [
    { required: true, message: $t('user.enterExpiration'), trigger: 'brue' }
  ]
}
onMounted(() => {
  onGetData()
})

// 获取数据
const dataForm = ref({
  expireYear: 1,
  scoreExpireSwitch: false
})
const onGetData = () => {
  nextTick(() => {
    http({
      url: http.adornUrl('/user/scoreExpire/info/' + 'SCORE_EXPIRE'),
      method: 'get',
      params: http.adornParams()
    }).then(({ data }) => {
      if (data !== null) {
        dataForm.value = data
      }
    })
  })
}
// 提交表单
const dataFormRef = ref(null)

const onSubmit = () => {
  dataFormRef.value.validate((valid) => {
    if (valid) {
      http({
        url: http.adornUrl('/user/scoreExpire'),
        method: 'post',
        data: http.adornData({
          paramKey: 'SCORE_EXPIRE',
          expireYear: dataForm.value.expireYear,
          scoreExpireSwitch: dataForm.value.scoreExpireSwitch,
          remark: $t('user.expirationConfiguration')
        })
      }).then(() => {
        onGetData()
        ElMessage({
          message: $t('publics.operation'),
          type: 'success',
          duration: 1500
        })
      }).catch(() => {

      })
    }
  })
}

</script>
